//package com.ksd.redis.config;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.JsonTypeInfo;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.DeserializationFeature;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.cache.RedisCacheConfiguration;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.cache.RedisCacheWriter;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializationContext;
//import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
//import java.time.Duration;
//
//@Configuration
//public class RedisConfig {
//
//    @Bean
//    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        RedisSerializer<String> redisSerializer = new StringRedisSerializer();//使用字符串序列化
//        redisTemplate.setConnectionFactory(factory);
//        redisTemplate.setKeySerializer(redisSerializer);    //key的序列化设置
//        redisTemplate.setHashKeySerializer(redisSerializer);//hash的序列化设置
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer());  //value的序列化设置
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer());//hashvalue的序列化设置
//        return redisTemplate;
//    }
//
//    /**
//     * 配置EnableCaching产生的redis乱码
//     */
//    @Bean
//    public RedisCacheManager redisCacheManager(RedisConnectionFactory factory){
//        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(factory);
//        RedisSerializationContext.SerializationPair<Object> pair =
//                RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer());
//        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
//                .serializeValuesWith(pair)
//                .entryTtl(Duration.ofSeconds(10))//设置10秒过期
//                ;
//        return new RedisCacheManager(redisCacheWriter,defaultCacheConfig);
//    }
//
//
//    private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(){
//        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
//        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
//        serializer.setObjectMapper(objectMapper);
//        return serializer;
//    }
//
//
//}
